package com.rabincorp.tactile.servlet;

import com.rabincorp.tactile.common.TactileCommon;
import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.rabincorp.tactile.entity.YouTubeVideoItem;
import com.rabincorp.tactile.feed.YouTubeFeed;

public class HomeServlet extends HttpServlet {

	private static final long serialVersionUID = 5576743748678746036L;
	
	private static final Logger LOGGER = Logger.getLogger(HomeServlet.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req,resp);
	}

	
	private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		YouTubeFeed youtubeGrabber = YouTubeFeed.getInstance();
		List<YouTubeVideoItem> videoList = youtubeGrabber.parse(TactileCommon.GAGS_CHANNEL);
		LOGGER.info("Video Size=" + videoList.size());
		
		request.setAttribute("videoList", videoList);

		RequestDispatcher dispatcher = request.getRequestDispatcher("welcome.jsp");
		dispatcher.forward(request, response);		
	}
	
}
